import triton
import triton.language as tl
import test_common
import pytest
import torch
import torch_npu
import numpy as np
import random
EPSILON = 1e-5

@triton.jit
def rmsnorm_kernel(
    x_ptr,
    x_row_stride,
    y_ptr,
    y_row_stride,
    n_rows,
    n_cols,
    BLOCK_SIZE: tl.constexpr,
    EPSILON: tl.constexpr = 1e-5,
):
    row_start = tl.program_id(0)
    row_step = tl.num_programs(0)
    for row_idx in tl.range(row_start, n_rows, row_step):
        x_ptr_base = x_ptr + row_idx * x_row_stride
        y_ptr_base = y_ptr + row_idx * y_row_stride
        idx_block = tl.arange(0, BLOCK_SIZE)
        x = tl.load(x_ptr_base + idx_block, mask=idx_block < n_cols, other=0.0)
        x = x.to(tl.float32)
        mean_square = tl.sum(x * x, axis=0) / n_cols
        rsqrt_mean_square = 1 / tl.math.sqrt(mean_square + EPSILON)
        ret = x * rsqrt_mean_square
        tl.store(y_ptr_base + idx_block, ret.to(y_ptr.dtype.element_ty), mask=idx_block < n_cols)

def rmsnorm(x):
    dtype = x.dtype
    BYTES = 48 * 1024 # 192 * 1024 / 4
    MAX_BLOCK_SIZE = BYTES // (4 if dtype == torch.float32 else 2)
    n_rows, n_cols = x.shape
    BLOCK_SIZE = triton.next_power_of_2(n_cols)
    y = torch.empty_like(x).npu()
    num_programs = 40
    num_programs = min(num_programs, n_rows)
    rmsnorm_kernel[(num_programs, 1, 1)](
        x,
        x.stride(0),
        y,
        y.stride(0),
        n_rows,
        n_cols,
        BLOCK_SIZE=BLOCK_SIZE
    )
    return y